'''
Created on 19/03/2010

@author: eh01
'''
from datastore.resource import Resource
import httplib
import logging
from runtime.service import Service
from datastore.uri import Uri

class LoginService(Service):
    """This class is a daemon task. It processes requests from httpserver::RequestHandler
    and return Spaces resources.
    """

    def cb_start(self, uri, action, res):      
        # process the post queues
        self.spaces.set_process(Uri('external:login'), self.cb_login)
        
#    def cb_login_submit(self, method, uri, return_value, result, context, **kwargs):
#        """This method handles the login submit.
#        
#        :param method: <class> or <type>
#        :param uri: The task Uri.
#        :param return_value: Resource
#        :param result: <class> or <type>
#        :param context: <class> or <type>
#        :param kwargs: <class> or <type>
#        """
#        
#        rs = Resource()
#        post_data = return_value.get_var(Resource.KEY_URI_SERVICES_TYPE_HTTP_REQUEST_DATA)
#        username = post_data['username']
#        password = post_data['password']
#        # hardcoded username and password. We aren't trying to demonstrate how to do 
#        # authentication in this reference implementation.
#        if username and username == 'HAF' and password and password == 'eDA':
#            rs.http_response_code = httplib.FOUND
#            rs.http_headers = {'Location': '/menu'}
#        else:
#            rs.set_var(Resource.KEY_URI_SERVICES_TYPE_HTTP_SITE_ACTION_RESULT, 'login')
#            rs.error_message = 'Incorrect username and password.'
#        
#        id = context.uuid()
#        request_uri = uri.requests.add(str(id))
#        context.put(request_uri.response, rs)
#        # return a copy of the uri as we dont want changes to the return uri by the caller to affect 
#        # how we use the uri
#        return (True, request_uri.uri_copy())        
#        
    def cb_login(self, uri, action, res):
        if action == 'get':
            res =  Resource(type="term:eplanning/reference/get/data")
            res.content = {}
            return res
        elif action == 'put':
            form_data = {}
            for key in res.form_fields.keys():
                form_data[key] = str(res.form_fields[key])
            username = form_data['username']
            password = form_data['password']
            # hardcoded username and password. We aren't trying to demonstrate how to do 
            # authentication in this reference implementation.
            if username and username == 'HAF' and password and password == 'eDA':
                rs.http_response_code = httplib.FOUND
                rs.http_headers = {'Location': '/menu'}
            else:
                rs.error_message = 'Incorrect username and password.'            
        elif action == 'exists':
            return True
        else:
            return